﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class RegistrarCliente : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    { 

    }

    String SQL, mssg;
    AdmiDatos.Service ad = new AdmiDatos.Service();
    System.Data.DataSet dst = new System.Data.DataSet();
    Formato1 vt = new Formato1();

    private void habilitarExiste()
    {
        BtnAgregar.Enabled = false;
        BtnModificar.Enabled = true;
        BtnEliminar.Enabled = true;
    }
    private void habilitarNuevo()
    {
        BtnAgregar.Enabled = true;
        BtnModificar.Enabled = false;
        BtnEliminar.Enabled = false;
    }
    private void deshabilitarBotones()
    {
        BtnAgregar.Enabled = false;
        BtnModificar.Enabled = false;
        BtnEliminar.Enabled = false;
        vt.LimpiartextBoxes(this);
    }

    protected void BtnAgregar_Click(object sender, EventArgs e)//CORREGIR
    {

        //if (Session["Idusuario"] = "Cl")
        try
        {
            SQL = "Insert into Clientes (UsuarioId,Nombre,Apellido,Direccion,Telefono,TipoId,Clave,Email)";
            SQL += "Values('"+ TxtIdUser.Text + "','" + TxtNombreUser.Text + "','" + TxtApellido.Text + "','" + TxtDireccion.Text + "','";
            SQL += TxtTelf.Text + "','" +TxtBTipoID.Text +"','"+ TxtClaveUser.Text + "','" + TxtEmail.Text + "')";
            Response.Write(SQL);
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("El Registro ha sido ingresado exitosamente!", this);
            deshabilitarBotones();
            
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }

    protected void BtnModificar_Click(object sender, EventArgs e)
    {
        try
        {
            SQL = "Update Clientes set Nombre ='" + TxtNombreUser.Text + "'";
            SQL += ",Apellido ='" + TxtApellido.Text + "'";
            SQL += ",Direccion = '" + TxtDireccion.Text + "'";
            SQL += ",Telefono = '" + TxtTelf.Text + "'";
            SQL += ",Clave = '" + TxtClaveUser.Text + "'";
            SQL += ",Email= '" + TxtEmail.Text + "'";
            SQL += "where  UsuarioId = '" + TxtIdUser.Text + "'"; 
            Response.Write(SQL);
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("El Registro ha sido modificado", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }
    protected void BtnEliminar_Click(object sender, EventArgs e)
    {
        try
        {
            SQL = "Delete from Clientes Where UsuarioId = '" + TxtIdUser.Text + "'";
            ad.actualizaD(SQL, ref mssg);
            vt.MessageBox("El Registro ha sido eliminado", this);
            deshabilitarBotones();
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }

    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            SQL = "Select * From Clientes Where UsuarioId = '" + TxtIdUser.Text + "'";
            ad.ObtenerD(ref mssg, SQL, ref dst);
            if (dst.Tables[0].Rows.Count > 0)
            {
                TxtNombreUser.Text = dst.Tables[0].Rows[0][1].ToString();
                TxtApellido.Text = dst.Tables[0].Rows[0][2].ToString();
                TxtDireccion.Text = dst.Tables[0].Rows[0][3].ToString();
                TxtTelf.Text = dst.Tables[0].Rows[0][4].ToString();
                TxtClaveUser.Text = dst.Tables[0].Rows[0][6].ToString();
                TxtEmail.Text = dst.Tables[0].Rows[0][7].ToString();
                habilitarExiste();
            }
            else
            {
                vt.MessageBox("Registro no encontrado!", this);
                habilitarNuevo();
                Session["Idusuario"] = TxtIdUser.Text;
                vt.LimpiartextBoxes(this);
                TxtIdUser.Text = Session["Idusuario"].ToString();
            }
        }
        catch (Exception ex)
        {
            vt.MessageBox(ex.Message, this);
        }
    }
}